//
//  AlbumHeaderView.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "AlbumHeaderView.h"

@interface AlbumHeaderView ()
@property (nonatomic, strong)CoreDataManager *manager;
@property (nonatomic, strong)NSArray *dataSource;
@property (nonatomic, assign)BOOL collectionState;
@property (nonatomic, strong)NSArray *deleteArray;
@property (nonatomic, assign)BOOL setState;

@end


@implementation AlbumHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, self.height - 20, self.height - 20)];
        [self addSubview:self.mainImageView];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.mainImageView.right + 10, 10, self.width - self.mainImageView.right - 20, 20)];
        
        [self addSubview:self.titleLabel];
        
        self.collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.collectionButton.frame = CGRectMake(self.mainImageView.right + 10, self.mainImageView.bottom - 30, 30, 30);
        
        
        
        
        [self addSubview:self.collectionButton];
        
        //收藏按钮的点击事件
        
        [self.collectionButton addTarget:self action:@selector(collectAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.collectionButton.right, self.collectionButton.top, (self.width - self.collectionButton.right) / 2 - 10, 30)];
        self.likeLabel.font = [UIFont systemFontOfSize:13];
        self.likeLabel.text = @"收藏本栏";
        self.likeLabel.backgroundColor = [UIColor blackColor];
        self.likeLabel.textColor = [UIColor whiteColor];
        self.likeLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.likeLabel];
        
        self.recommendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.recommendButton.frame  =CGRectMake(self.likeLabel.right + 10, self.likeLabel.top, self.likeLabel.width, self.likeLabel.height);
        [self.recommendButton setTitle:@"相关推荐" forState:UIControlStateNormal];
        
        self.recommendButton.titleLabel.font = [UIFont systemFontOfSize:13];
        
        self.recommendButton.backgroundColor = [UIColor blackColor];
        
        //相关推荐点击事件
        [self.recommendButton addTarget:self action:@selector(recommendAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.recommendButton];
        
        
        self.playTimesLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.titleLabel.bottom + 10, self.titleLabel.width, self.recommendButton.top - self.titleLabel.bottom - 20)];
        self.playTimesLabel.numberOfLines = 0;
        self.playTimesLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.playTimesLabel];
        
        //打印路径
        self.manager = [CoreDataManager shareManager];
        NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
        //判断按钮状态
       
        [self.collectionButton setBackgroundImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
        
        [self selectAllData];
        
    
    }
    return self;
}




- (void)setAlbumModel:(AlbumModel *)albumModel {
    if (_albumModel != albumModel) {
        _albumModel = albumModel;
    }
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.albumModel.coverMiddle]];
    self.titleLabel.text = self.albumModel.title;
    if (self.albumModel.intro == nil) {
        self.playTimesLabel.text = @"暂无简介";
    } else {
        self.playTimesLabel.text = self.albumModel.intro;
    }
    
    
    //判断按钮状态,让这个方法只走一次
    
    if (!self.setState) {
        for (AlbumList *albumList in self.dataSource) {
            NSLog(@"%@", albumList.url);
            if ([albumList.url isEqualToString:[NSString stringWithFormat:@"%@", self.albumModel.albumId]]) {
                [self.collectionButton setBackgroundImage:[UIImage imageNamed:@"yishoucang"] forState:UIControlStateNormal];
                self.collectionState = YES;
            }
        }
        self.setState = !self.setState;
    }
}




//收藏按钮的点击方法

- (void)collectAction:(UIButton *)sender {
    [self selectAllData];
    if (self.collectionState == YES) {
        [sender setBackgroundImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
        
        
        //已收藏，删除收藏
        
        for (AlbumList *albumList in self.dataSource) {
            if ([albumList.url isEqualToString:[NSString stringWithFormat:@"%@", self.albumModel.albumId]]) {
                [self.manager.managedObjectContext deleteObject:albumList];
            }
        }
        [self.manager saveContext];
        self.collectionState = NO;
    } else {
        [sender setBackgroundImage:[UIImage imageNamed:@"yishoucang"] forState:UIControlStateNormal];
        self.collectionState = YES;
        //未收藏，增加收藏
        AlbumList *albumList = [NSEntityDescription insertNewObjectForEntityForName:@"AlbumList" inManagedObjectContext:self.manager.managedObjectContext];
        albumList.image = self.albumModel.coverMiddle;
        albumList.title = self.albumModel.title;
        albumList.url = [NSString stringWithFormat:@"%@", self.albumModel.albumId];
        [self.manager saveContext];

    }
}

//查询所有

- (void)selectAllData {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    //设置要请求的实体
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"AlbumList" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    self.dataSource = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (self.dataSource == nil) {
        NSLog(@"%@", error);
    }
}









//相关推荐点击

- (void)recommendAction:(UIButton *)sender {

    self.block(self.albumModel.albumId);
    
    
    
    
}


@end
